package cn.hs.ph.servlet;

import cn.hs.ph.dao.CommunityDao;
import cn.hs.ph.dao.impl.CommunityDaoImpl;
import cn.hs.ph.dao.impl.RelevancyShopDaoImpl;
import cn.hs.ph.entity.Community;
import cn.hs.ph.entity.Shop;
import cn.hs.ph.util.Result;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/community")
public class CommunityServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
//        小区操作对象
        CommunityDaoImpl communityDao = new CommunityDaoImpl();
//        关联门店操作对象
        RelevancyShopDaoImpl relevancyShopDao = new RelevancyShopDaoImpl();
//        响应对象
        PrintWriter respWriter = resp.getWriter();
        /*分类处理请求*/
//        查看全部
        if ("selectAll".equals(action)) {
//        请求参数
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            String communityName = req.getParameter("communityName");
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
//        参数封装
            Map<String, String> params = new HashMap<>();
            params.put("pageNum", pageNum);
            params.put("pageSize", pageSize);
            params.put("communityName", communityName);
            params.put("startTime", startTime);
            params.put("endTime", endTime);
//        总条数
            int total = communityDao.selectCount(params);
//            最终响应
            List<Community> list = communityDao.selectAll(params);
            Map<String, Object> resMap = new HashMap<>();
            resMap.put("list", list);
            resMap.put("total", total);
            Result data = Result.success(resMap);
            respWriter.write(JSON.toJSONString(data));
        }
//        添加/修改
        else if ("add".equals(action)) {
//                添加
            if ("1".equals(req.getParameter("addOrUpdateFlag"))) {
                Community community = new Community();
//        name
                community.setCommunityName(req.getParameter("communityName"));
//        省市区地点
                community.setCommunityProvince(req.getParameter("communityProvince"));
                community.setCommunityCity(req.getParameter("communityCity"));
                community.setCommunityDistrict(req.getParameter("communityDistrict"));
                community.setCommunityAddress(req.getParameter("communityAddress"));
//        经纬
                community.setCommunityLatitude(req.getParameter("communityLatitude"));
                community.setCommunityLongitude(req.getParameter("communityLongitude"));
//        关联门店
                String shopCount = req.getParameter("communityShopCount");
                int communityShopCount = 0;
                if (shopCount != null && !"".equals(shopCount)) {
                    communityShopCount = Integer.parseInt(shopCount);
                }
                community.setCommunityShopCount(communityShopCount);
//        用户数量
                String userCount = req.getParameter("communityShopCount");
                int communityUserCount = 0;
                if (userCount != null && !"".equals(userCount)) {
                    communityUserCount = Integer.parseInt(userCount);
                }
                community.setCommunityUserCount(communityUserCount);
//        最终添加
                int row = communityDao.insert(community);
                if (row == 1) {
                    respWriter.write(JSON.toJSONString(Result.success("添加成功")));
                } else {
                    respWriter.write(JSON.toJSONString(Result.error("添加失败")));
                }
            }
//                修改
            else if ("2".equals(req.getParameter("addOrUpdateFlag"))) {
                Community community = new Community();
//        id name
                community.setCommunityId(Integer.parseInt(req.getParameter("communityId")));
                community.setCommunityName(req.getParameter("communityName"));
//        省市区地点
                community.setCommunityProvince(req.getParameter("communityProvince"));
                community.setCommunityCity(req.getParameter("communityCity"));
                community.setCommunityDistrict(req.getParameter("communityDistrict"));
                community.setCommunityAddress(req.getParameter("communityAddress"));
//        经纬
                community.setCommunityLatitude(req.getParameter("communityLatitude"));
                community.setCommunityLongitude(req.getParameter("communityLongitude"));
                int row = communityDao.update(community);
                if (row == 1) {
                    respWriter.write(JSON.toJSONString(Result.success("修改成功")));
                } else {
                    respWriter.write(JSON.toJSONString(Result.error("修改失败")));
                }
            }
        }
//        删除
        else if ("delect".equals(action)) {
            String id = req.getParameter("communityId");
            if (id != null && !"".equals(id)) {
                int communityId = Integer.parseInt(id);
                communityDao.delete(communityId);
                respWriter.write(JSON.toJSONString(Result.success("删除成功")));
            } else {
                respWriter.write(JSON.toJSONString(Result.error("删除失败")));
            }
        }
        /*关联门店*/
//        显示
        else if ("shopShow".equals(action)) {
//        请求参数
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            String communityId = req.getParameter("communityId");
            String shopName = req.getParameter("shopName");
//            请求参数封装
            Map<String, String> params = new HashMap<>();
            params.put("pageNum", pageNum);
            params.put("pageSize", pageSize);
            params.put("communityId", communityId);
            params.put("shopName", shopName);
//            总条数
            int total = relevancyShopDao.shopShowCount(params);
//            最终响应
            List<Shop> shops = relevancyShopDao.shopShow(params);
            Map<String, Object> resMap = new HashMap<>();
            resMap.put("list", shops);
            resMap.put("total", total);
            Result data = Result.success(resMap);
            respWriter.write(JSON.toJSONString(data));
        }
//        删除
        else if ("shopDelect".equals(action)) {
//            关联门店ID
            String panghuShopId = req.getParameter("panghuShopId");
            String id = req.getParameter("panghuCommunityId");
            int communityId=Integer.parseInt(id);
            int shopId = Integer.parseInt(panghuShopId);
            int row = relevancyShopDao.shopUpdate(shopId, 0);
            if (row != 0) {
                int num = communityDao.updateShopCount(-row, communityId);
                if(num!=0){
                    respWriter.write(JSON.toJSONString(Result.success("删除成功")));
                }else {
                    respWriter.write(JSON.toJSONString(Result.error("删除失败")));
                }
            } else {
                respWriter.write(JSON.toJSONString(Result.error("删除失败")));
            }
        }
//        添加关联门店显示
        else if ("shopAddShow".equals(action)) {
//        请求参数
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            String shopName = req.getParameter("shopName");
//            请求参数封装
            Map<String, String> params = new HashMap<>();
            params.put("pageNum", pageNum);
            params.put("pageSize", pageSize);
            params.put("shopName", shopName);
//            总条数
            int total = relevancyShopDao.shopAddShowCount(params);
//            最终响应
            List<Shop> shops = relevancyShopDao.shopAddShow(params);
            Map<String, Object> resMap = new HashMap<>();
            resMap.put("list", shops);
            resMap.put("total", total);
            Result data = Result.success(resMap);
            respWriter.write(JSON.toJSONString(data));
        }
//        添加
        else if ("shopAdd".equals(action)) {
//            小区ID
            String panghuCommunityId = req.getParameter("panghuCommunityId");
            int communityId = Integer.parseInt(panghuCommunityId);
//            关联门店ID
            String[] fendeShopCommunityList = req.getParameterValues("fendeShopCommunityList[]");
            for (String s:fendeShopCommunityList) {
                int shopId = Integer.parseInt(s);
                int num = relevancyShopDao.shopUpdate(shopId, communityId);
                communityDao.updateShopCount(num,communityId);
            }
            respWriter.write(JSON.toJSONString(Result.success("添加成功")));
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
